package com.example.smartcity_poverty.ui.AllService.SeekHouse.activity

import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.ServiceBean.SeekHouseBean.SHHotListAllModel
import com.example.smartcity_poverty.logic.ServiceBean.SeekHouseBean.SHHotListShowDataModel
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import kotlinx.android.synthetic.main.activity_s_h_show_data.*
import kotlinx.coroutines.Dispatchers

/**
 * 信息详情页
 */
class SHShowDataActivity : AppCompatActivity() {

    private val id by lazy { intent.getStringExtra("id")?.toInt() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_s_h_show_data)
        title = "详情页"

        var phone: String? = null

        // 发起网络请求
        coroutine(Dispatchers.Main) {
            api.getSHHotListShowData(id!!).apply {
                if (this.code == "200") {
                    runOnUiThread {
                        this.data?.let {
                            glide(url + it.pic, shhImage)
                            shhTitle.text = it.sourceName
                            shhSize.text = "建筑面积: ${it.areaSize}㎡"
                            shhMoney.text = "房源单价: ${it.price}"
                            shhClass.text = "房源类型: ${it.houseType}"
                            shhContent.text = "房源介绍: ${it.description}"
                            phone = it.tel
                        }
                    }
                } else runOnUiThread { this.msg?.show() }
            }
        }

        shhFinish.setOnClickListener { finish() }
        shhPhone.setOnClickListener {
            val intent = Intent(Intent.ACTION_DIAL)
            intent.data = Uri.parse("tel: $phone")
            startActivity(intent)
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}